package com.woniuxy.ticketfile.controller;/*
@author houguai
@create 2021-02-25 19:18
*/

import com.woniuxy.ticketcommon.ResponceResult;
import com.woniuxy.ticketfile.util.AliyunOSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Api("阿里云OSS文件上传、下载、删除API")
@RestController
@RequestMapping("/oss")
@Slf4j
public class FileController {

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;


    @ApiOperation(value = "文件上传")
    @PostMapping("/uploadfile")
    public ResponceResult<Object> fileUpload(@RequestParam("file") MultipartFile file)
    {
        ResponceResult<Object> responceResult=new ResponceResult<>();
        try
        {

            String url = aliyunOSSUtil.upload(file); //2、调用

            responceResult.setData(url);
            responceResult.setCode(200);
            responceResult.setMsg("ok");
            return responceResult;
        }
        catch (Exception e)
        {
           responceResult.setCode(401);
           responceResult.setMsg("上传失败");
            return  responceResult;
        }

    }


    /**
     * 文件下载api
     * @param fileName
     * @param response
     * @return  ResponceResult<Object>
     * @throws Exception
     */
    @ApiOperation(value = "文件下载")
    @GetMapping(value = "download/{fileName}")
    public ResponceResult<Object> download(@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {
        ResponceResult<Object> responceResult=new ResponceResult<>();
        try
        {

            String url = aliyunOSSUtil.download(fileName,response); //2、调用

            responceResult.setData(url);
            responceResult.setCode(200);
            responceResult.setMsg("ok");
            return responceResult;
        }
        catch (Exception e)
        {
            responceResult.setCode(401);
            responceResult.setMsg("下载失败");
            return  responceResult;
        }

    }

    /*
     * 文件删除api
     * @param: fileName
     * @return: com.alibaba.fastjson.JSONObject
     * @create: 2020/10/31 17:35
     * @author: csp1999
     */
    @ApiOperation(value = "文件删除")
    @GetMapping("/delete/{fileName}")
    public ResponceResult<Object> DeleteFile(@PathVariable("fileName") String fileName) {
        ResponceResult<Object> responceResult=new ResponceResult<>();

        try
        {

            String url = aliyunOSSUtil.delete(fileName); //2、调用

            responceResult.setData(url);
            responceResult.setCode(200);
            responceResult.setMsg("ok");
            return responceResult;
        }
        catch (Exception e)
        {
            responceResult.setCode(401);
            responceResult.setMsg("上传失败");
            return  responceResult;
        }
}


}
